/**
 * 该插件用于将存在 vuex player 相关状态同步到， $music 播放器上
 * @author ali 
 * @desc 监听每次 mutation 之后调用，把相关 state 同步到 $music 里
 */
import consts from 'Service/store/const'

export default  store => {
  store.subscribe((mutation, state) => {
    let $music = GLOBAL.vbus.$music;

    switch (mutation.type) {
        case consts.SETPLAYLIST:
            $music.list = state.playerList;
            break;
        case consts.SETPLAYURL:
            mutation.payload.forEach(item => {
                let trackIndex = $music.list.findIndex(i => i.id == item.id);

                if(trackIndex == -1)return;

                $music.list[trackIndex].src = item.url;

            });
            break    
        case consts.SETCURRENTPLAYINDEX:
                $music.index = mutation.payload; 
                $music.change();
            break;
        default:
            break;
    }
    
    // console.log($music.list)
  })
}